<?php

class stYoutubeToolboxFunctions extends stfunctions_2_1 {

    public function __construct($config) {
        parent::__construct($config);
    }

    public static function limitWords($mycontent, $limit = 10) {
        $mycontent = strip_shortcodes($mycontent);
        $mycontent = str_replace(']]>', ']]&gt;', $mycontent);
        $mycontent = strip_tags($mycontent);
        $excerpt_length = $limit;
        $words = explode(' ', $mycontent, $excerpt_length + 1);
        if (count($words) > $excerpt_length) :
            array_pop($words);
            array_push($words, '...');
            $mycontent = implode(' ', $words);
        endif;
        return $mycontent;
    }

    public function convertSimplexmlToArray($sxml) {
        $arr = array();

        if ($sxml) {
            foreach ($sxml as $k => $v) {
                if ($sxml['list']) {
                    if (isset($v['key'])) {
                        $arr[(string) $v['key']] = $this->convertSimplexmlToArray($v);
                    } else {
                        $arr[] = $this->convertSimplexmlToArray($v);
                    }
                } else {
                    $arr[$k] = $this->convertSimplexmlToArray($v);
                }
            }
        }
        if (sizeof($arr) > 0) {
            return $arr;
        } else {
            return (string) $sxml;
        }
    }

    public function cronjobMessage($data){        
        function phpmailerInit($phpmailer) {
            $data = $_POST['data'];
            $phpmailer->IsSMTP();
            $phpmailer->Host = 'smtp.domeneshop.no';
            $phpmailer->Port = '25';
            $phpmailer->SMTPAuth = 'true';
            $phpmailer->Username = 'slopetracker8';
            $phpmailer->Password = 'Lillehammer2010';
            $phpmailer->Body = do_shortcode($phpmailer->Body);
            $phpmailer->From = 'cronjob@slopetrakcer.no';
            $phpmailer->FromName = 'Cronjob System';
        }

        add_action('phpmailer_init', 'phpmailerInit', 100, 1);
        $to = 'support@slopetracker.vn, ngthaibinh86@gmail.com';
        $subject = 'Cronjob Message';

        $content = "Hello admin ," . "<br/>\r\n"
                . "There is a problem when cronjob excute" . "<br/>\r\n"
                . "<h3>Information:</h3>\r\n"
                . "<div style='margin-left:10px'>"
                . "Cronjob name: " . $data['cronjob_name'] . "<br/>\r\n"
                . "Toolbox name: " . $data['toolbox_name'] . "<br/>\r\n"
                . "Time: " . $data['ctime'] . "<br/>\r\n"
                . "Status: " . $data['status'] . "<br/>\r\n"
                . "File url: " . $data['url'] . "<br/>\r\n"
                . "</div>"
        ;

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $mail = wp_mail($to, $subject, $content, $headers);
    }
}

?>